﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

using Ext.Net;
using Ext.Net.Utilities;

namespace EVegetable.WebAdmin
{
    using EVegetable.Common.Constant;
    using EVegetable.Common.Web;

    using EVegetable.Domain;
    using EVegetable.Service;

    public partial class Main : BaseWebPage
    {
        private const string MenuClickedHandler = "if (node.attributes.href) { e.stopEvent(); loadPage(#{tabContent}, node); }";

        protected void Page_Load(object sender, EventArgs e)
        {
            List<TSys_AppMenu> menuList = this.GetAppMenu();

            this.InitLeftMenuLevel0(menuList);

            if (!X.IsAjaxRequest)
            {
                string text = @"Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Sed metus nibh, sodales a, porta at, vulputate eget, dui. Pellentesque ut nisl. Maecenas tortor turpis, interdum non, sodales non, iaculis ac, lacus. Vestibulum auctor, tortor quis iaculis malesuada, libero lectus bibendum purus, sit amet tincidunt quam turpis vel lacus. In pellentesque nisl non sem. Suspendisse nunc sem, pretium eget, cursus a, fringilla vel, urna.";

                this.ResourceManager1.RegisterClientScriptBlock("text", string.Format("var text=\"{0}\";", text));

                foreach (Portlet portlet in ControlUtils.FindControls<Portlet>(this.Page))
                {
                    portlet.Html = "={text}";
                    portlet.Padding = 5;
                    portlet.Tools.Add(new Tool(ToolType.Close, string.Concat(portlet.ClientID, ".hide();"), "Close Portlet"));
                }
            }

            foreach (Portlet portlet in ControlUtils.FindControls<Portlet>(this.Page))
            {
                portlet.DirectEvents.Hide.Event += Portlet_Hide;
                portlet.DirectEvents.Hide.EventMask.ShowMask = true;
                portlet.DirectEvents.Hide.EventMask.Msg = "Saving...";
                portlet.DirectEvents.Hide.EventMask.MinDelay = 500;

                portlet.DirectEvents.Hide.ExtraParams.Add(new Ext.Net.Parameter("ID", portlet.ClientID));
            }
        }

        protected void Portlet_Hide(object sender, DirectEventArgs e)
        {
            X.Msg.Alert("Status", e.ExtraParams["ID"] + " Hidden").Show();
        }

        private List<TSys_AppMenu> GetAppMenu()
        {
            TSys_AppMenuSvc appMenuSvc = new TSys_AppMenuSvc();

            List<TSys_AppMenu> allMenuList = appMenuSvc.GetAllList().Where(p => p.Active = true).ToList();

            return allMenuList;
        }

#region Operate Node

        private void InitLeftMenuLevel0(List<TSys_AppMenu> menuList)
        {
            Ext.Net.Panel panel = null;

            List<TSys_AppMenu> level0 = menuList.Where(p => p.MenuLevel == MenuLevel.Level0).OrderBy(p=>p.Sort).ToList();

            foreach (TSys_AppMenu menu in level0)
            {
                panel = new Ext.Net.Panel()
                {
                    ID = string.Format("panel{0}", menu.MenuID),
                    Title = menu.MenuName,
                    Icon = Icon.FolderGo
                };

                this.InitLeftMenu(panel, menu.MenuID, menuList);

                this.pnlMainMenu.Items.Add(panel);
            }
        }

        private void InitLeftMenu(Ext.Net.Panel panel, int pMenuID, List<TSys_AppMenu> menuList)
        {
            List<TSys_AppMenu> listLevel1 = menuList.Where(p => p.PMenuID == pMenuID).OrderBy(p => p.Sort).ToList();

            TreePanel treePanel = new TreePanel() { 
                ID = string.Format("treePanel{0}", pMenuID),
                Icon = Icon.BookOpen,
                Border = false,
                AutoHeight = true,
                RootVisible = false
            };

            if (listLevel1.Count == 0) return;

            Ext.Net.TreeNode rootNode = new Ext.Net.TreeNode()
            {
                NodeID = pMenuID.ToString()
            };

            foreach (TSys_AppMenu menu in listLevel1)
            {
                // Create menu for level 1
                Ext.Net.TreeNode node = new Ext.Net.TreeNode();

                if (!string.IsNullOrEmpty(menu.LinkURL)) 
                { 
                    node.Href = this.Page.ResolveUrl(menu.LinkURL);
                    node.Listeners.Click.Handler = MenuClickedHandler;
                }
                node.NodeID = menu.MenuID.ToString();
                node.Text = menu.MenuName;
                node.Qtip = menu.Description;
                

                // Create menu for level 2
                List<TSys_AppMenu> listLevel2 = menuList.Where(p => p.PMenuID == menu.MenuID).OrderBy(p => p.Sort).ToList();
                foreach (TSys_AppMenu subMenu in listLevel2)
                {
                    Ext.Net.TreeNode subNode = new Ext.Net.TreeNode();

                    if (!string.IsNullOrEmpty(subMenu.LinkURL))
                    {
                        subNode.Href = this.Page.ResolveUrl(subMenu.LinkURL);
                        subNode.Listeners.Click.Handler = MenuClickedHandler;
                    }
                    subNode.NodeID = subMenu.MenuID.ToString();
                    subNode.Text = subMenu.MenuName;
                    subNode.Qtip = subMenu.Description;
                    

                    node.Nodes.Add(subNode);
                }

                rootNode.Nodes.Add(node);
            }

            treePanel.Root.Add(rootNode);
            panel.Items.Add(treePanel);
        }

        //static node creation with children
        private Ext.Net.TreeNode CreateNode(SiteMapNode siteMapNode)
        {
            Ext.Net.TreeNode treeNode = new Ext.Net.TreeNode();

            if (!string.IsNullOrEmpty(siteMapNode.Url))
            {
                treeNode.Href = this.Page.ResolveUrl(siteMapNode.Url);
            }

            treeNode.NodeID = siteMapNode.Key;
            treeNode.Text = siteMapNode.Title;
            treeNode.Qtip = siteMapNode.Description;

            SiteMapNodeCollection children = siteMapNode.ChildNodes;

            if (children != null && children.Count > 0)
            {
                foreach (SiteMapNode mapNode in siteMapNode.ChildNodes)
                {
                    treeNode.Nodes.Add(this.CreateNode(mapNode));
                }
            }

            return treeNode;
        }
#endregion
    }
}
